package com.ecar.common.util;

import net.sf.json.JSONObject;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import java.util.Map;

/**
 * JSON序列化工具类
 * @author admin
 */
public class JSONUtil {
	
	public static String serialize(Object obj) {
		return serialize(obj, false);
	}
	
	public static String serialize(Object obj, boolean pretty) {
		ObjectMapper om = new ObjectMapper();
		String result = null;
		try {
			SimpleFilterProvider sp = new SimpleFilterProvider();
			sp.setFailOnUnknownId(false);
			om.setFilters(sp);
			if (pretty) {
				result = om.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
			} else {
				result = om.writeValueAsString(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String serializeInclude(Object obj, String... fields) {
		if (fields.length < 0) return null;
		else if (fields.length == 0) return serialize(obj, false);
		
		String result = null;
		ObjectMapper om = new ObjectMapper();

		SimpleBeanPropertyFilter sbpf = SimpleBeanPropertyFilter.filterOutAllExcept(fields);
		SimpleFilterProvider sp = new SimpleFilterProvider().addFilter("fieldFilter", sbpf);
		sp.setFailOnUnknownId(false);
		om.setFilters(sp);
		try {
			result = om.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String serializeAll(Object obj) {
		String result = null;
		com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();
		com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider sp
				= new com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider();
		sp.setFailOnUnknownId(false);
		om.setFilters(sp);

		try {
			result = om.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String serializeExclude(Object obj, String... fields) {
		String result = null;
		ObjectMapper om = new ObjectMapper();
		
		om.setSerializationInclusion(Inclusion.NON_NULL);
		SimpleBeanPropertyFilter sbpf = SimpleBeanPropertyFilter.serializeAllExcept(fields);
		SimpleFilterProvider sp = new SimpleFilterProvider().addFilter("fieldFilter", sbpf);
		sp.setFailOnUnknownId(false);
		om.setFilters(sp);
		try {
			result = om.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static <T> T deserialize(String json, Class<T> classType) {
		T obj = null;
		ObjectMapper om = new ObjectMapper();
		
		om.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			obj = om.readValue(json, classType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	public static <T> T deserialize(JSONObject json, Class<T> classType) {
		return deserialize(json.toString(), classType);
	}

	public static String serializeMap(Map map) {
		String result = null;
		ObjectMapper om = new ObjectMapper();
		
		try {
			result = om.writeValueAsString(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
